#include "debug.h"
#include "Demura.h"
#include "../DDIC/AllDDIC.h"
#include "usbd_cdc.h"
#include "fsmc.h"

/********************************************************************************************
Write_Demura_Data_to_RAM函数作为补偿资料载入Demura RAM的统一入口   
u8 buf2PC[10];
DemuraIctype->IC型号; Demura_OLEDModule_flag->项目号	
编号同上位机Demura通用界面下的编号顺序相对应
编号0 ->  R66455_GM820(639)
编号1 ->  R66456_GM825(639) 
(详细查询上位机Demura界面Project集合)

Demura_OLEDModule_flag=USB_Rx_Buffer[1]	; 
********************************************************************************************/
void Write_Data_to_Demura_RAM(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8 buffer[],u16 LP_B7_Data,u16 HS_B7_Data)
{			
		switch(Demura_OLEDModule_flag)
		{
			case R66455_GM820:  //编号：0
				R66455_GM820_Write_Demura_Ram(pdev,CDC_IN_EP,OLED.SigMode ,channel,buffer,LP_B7_Data,HS_B7_Data); break;		
			case R66456_GM825:  //编号：1
				R66456_GM825_Write_Demura_Ram(pdev,CDC_IN_EP,OLED.SigMode ,channel,buffer,LP_B7_Data,HS_B7_Data); break;		
			case RM692A6_GM830: //编号：2
				RM692A6_GM830_Write_Demura_Ram(pdev,CDC_IN_EP,OLED.SigMode ,channel,buffer,LP_B7_Data,HS_B7_Data); break;	
			case RM692A9_GM870: //编号：3
				RM692A9_GM870_Write_Demura_Ram(pdev,CDC_IN_EP,OLED.SigMode ,channel,buffer,LP_B7_Data,HS_B7_Data); break;				
			case RM69298_GM760: //编号：6
				RM69298_GM760_Write_Demura_Ram(pdev,CDC_IN_EP,OLED.SigMode ,channel,buffer,LP_B7_Data,HS_B7_Data); break;		
			case RM69350_GM620: //编号：7
				RM69350_GM620_Write_Demura_Ram(pdev,CDC_IN_EP,OLED.SigMode ,channel,buffer,LP_B7_Data,HS_B7_Data); break;		
			case NT37700C_GM820://编号：8
				NT37700C_GM820_Write_Demura_Ram(pdev,CDC_IN_EP,OLED.SigMode ,channel,buffer,LP_B7_Data,HS_B7_Data); break;	
			case R66451_ALPHA:	//编号：15
				R66451_ALPHA_Write_Demura_Ram(pdev,CDC_IN_EP,OLED.SigMode ,channel,buffer,LP_B7_Data,HS_B7_Data); break;
			case SD5202_GM583:   			//编号：16
				SD5202_GM583_Write_Demura_Ram(pdev,CDC_IN_EP,OLED.SigMode ,channel,buffer,LP_B7_Data,HS_B7_Data); break;
			case RM69351_GM920: //编号：17		
				RM69351_GM920_Write_Demura_Ram(pdev,CDC_IN_EP,OLED.SigMode ,channel,buffer,LP_B7_Data,HS_B7_Data); break;
			case R66451_GM862:	//编号：18
				R66451_GM862_Write_Demura_Ram(pdev,CDC_IN_EP,OLED.SigMode ,channel,buffer,LP_B7_Data,HS_B7_Data); break;
			case RM692C9_GM828: //编号：20
				RM692C9_GM828_Write_Demura_Ram(pdev,CDC_IN_EP,OLED.SigMode ,channel,buffer,LP_B7_Data,HS_B7_Data); break;
			case RM692C9_GM930: //编号：24
				RM692C9_GM930_Write_Demura_Ram(pdev,CDC_IN_EP,OLED.SigMode ,channel,buffer,LP_B7_Data,HS_B7_Data); break;
			case RM692D7_GM920:	//编号：25
				RM692D7_GM920_Write_Demura_Ram(pdev,CDC_IN_EP,OLED.SigMode ,channel,buffer,LP_B7_Data,HS_B7_Data); break;
			case NT37800A_GM862://编号：26
				NT37800A_GM862_Write_Demura_Ram(pdev,CDC_IN_EP,OLED.SigMode ,channel,buffer,LP_B7_Data,HS_B7_Data); break;
			case NT37701A_GM040://编号：27
				NT37701A_GM040_Write_Demura_Ram(pdev,CDC_IN_EP,OLED.SigMode ,channel,buffer,LP_B7_Data,HS_B7_Data); break;
			case RM692EX_DemuraIP://编号：28
				RM692EX_DemuraIP_Write_Demura_Ram(pdev,CDC_IN_EP,OLED.SigMode ,channel,buffer,LP_B7_Data,HS_B7_Data); break;
			case RM692D9_GM930://编号：29
			  RM692Dx_GM930_Write_Demura_Ram(pdev,CDC_IN_EP,OLED.SigMode ,channel,buffer,LP_B7_Data,HS_B7_Data);break;
			default:
				RM692C9_GM828_Write_Demura_Ram(pdev,CDC_IN_EP,OLED.SigMode ,channel,buffer,LP_B7_Data,HS_B7_Data); break;
		}
}
/********************************************************************************************
以RGB顺序写入补偿资料
********************************************************************************************/
void SSD2828_W_RAM_demura_buffer_part_rgb(float pclk,u8 SigMode ,u8* buffer ,u16 length,u8  multiple ,u8 DisMode)
{
  	u32 i;
		u32 Data_Num = 0;
    Data_Num = length*multiple*3;
    if(SigMode == Mipi_Mode)
    {
        if(DisMode == 0)
        {
            for(i = 0;i<(Data_Num/2);i++)
            {
                Write_DATA(buffer[i<<1]+(buffer[(i<<1)+1]<<8));
                delay_6ns((pclk < 50) ? 50 : 0);
            } 
    }
		else if(DisMode == 1) 
        {                        
            while(Data_Num > 0)
            {
                for(i=0;i<length/2;i++)
                {
										Write_DATA(buffer[i*6+0]+(buffer[i*6+1]<<8));
										Write_DATA(buffer[i*6+2]+(buffer[i*6+3]<<8));
										Write_DATA(buffer[i*6+4]+(buffer[i*6+5]<<8));
										Data_Num-=6; 
                }                                       
            }
        }
 
    }
    else if(SigMode == CMD_Mode)
    {
		#ifdef SSD_Command_16
        for(i=0;i<(Data_Num/6);i++)
        {
            Write_DATA((buffer[i*6+1]<<8)+buffer[i*6+2]);	
						//delay_us(1);
            Write_DATA((buffer[i*6+5]<<8)+buffer[i*6]);	
						//delay_us(1);
            Write_DATA((buffer[i*6+3]<<8)+buffer[i*6+4]);		
						//delay_us(1);
        }
		#else	
        for(i=0;i<Data_Num/3;i++)
        {
            Write_DATA(buffer[i*3+2]);
            Write_DATA(buffer[i*3+1]);
            Write_DATA(buffer[i*3]);          
        }
		#endif
    }   
}

/********************************************************************************************
以BGR顺序写入补偿资料
********************************************************************************************/
void SSD2828_W_RAM_demura_buffer_part_bgr(float pclk,u8 SigMode ,u8* buffer ,u16 length,u8  multiple ,u8 DisMode)
{
  	u32 i;
    u32 Data_Num = 0;
    Data_Num = length*multiple*3;

	#ifdef SSD_Command_16
	for(i=0;i<(Data_Num/6);i++)
	{	
		Write_DATA((buffer[i*6+1]<<8)+buffer[i*6]);//210, 543
					delay_6ns(1);
		Write_DATA((buffer[i*6+5]<<8)+buffer[i*6+2]);
					delay_6ns(1);
		Write_DATA((buffer[i*6+3]<<8)+buffer[i*6+4]);								
					delay_6ns(1);	
	}
	#else	
	for(i=0;i<Data_Num/3;i++)
	{
		Write_DATA(buffer[i*3+2]);
		Write_DATA(buffer[i*3+1]);
		Write_DATA(buffer[i*3]);          
	}
	#endif 
}




///////////////////----
//以下违章搭建物尽快挪走

//void Demura2RAM(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8 buffer[],u16 LP_B7_Data,u16 HS_B7_Data)
//{
//	static u8 test0=0;
//	//u8 buf2PC[10];
//	/*DemuraIctype->IC型号; Demura_OLEDModule_flag->项目号*/
//	USB_Rx_Demura_End_Flag=0;	
//	
//	if(USB_Rx_Demura==0)//第一笔数据
//	{												
//		test0=0;
//		if(DemuraIctype==0x5C)//synapic
//		{
//			DemuraTransOnce=256*3;
//		}
//		else if(DemuraIctype==0x5D)//novateck
//		{
//			DemuraTransOnce=180*3;
//		}				
//		else //if(DemuraIctype==0x5B)   //raydium & viewtrix
//		{
//			DemuraTransOnce=OLED.H_pixel*3;
//		}
//		USB_Rx_Demura=1; //进入Demura数据写入状态														
//	}
//	for(i=0;i<USB_ReceivedCount;i++)
//	{
//		buffer[i+VCP_Receive_True_num] = USB_Rx_Buffer[i];  //
//	}
//	VCP_Receive_True_num+=USB_ReceivedCount;
//	DemuraRecivedNum    +=USB_ReceivedCount;				
//	//-----------------------------------------------------------------				
//	
//	if((VCP_Receive_True_num==DemuraTransOnce)||((DemuraRecivedNum==USB_Rx_Demura_Total_Num))) //写入单笔数据,单次写入DemuraTransOnce个字节
//	{												
//		if(DemuraIctype==0x5C)  //synapic
//		{
//			switch(Demura_OLEDModule_flag)
//			{		
//				case 0x00:   /////GM820-R66455  GM860	
//				case 0x02:
//				{
//					R66455_Flash_Write_Demura(pdev,OLED.SigMode,channel,DemuraRecivedNum,buffer,DemuraTransOnce);		
//					break;
//				}	
//				case 0x01:	
//				default:
//				{		
//					R66456_Flash_Write_Demura(pdev,OLED.SigMode,channel,DemuraRecivedNum,buffer,DemuraTransOnce);	
//					break;
//				}
//				case 0x03:		
//					R66451_Flash_Write_Demura(pdev,OLED.SigMode,channel,DemuraRecivedNum,buffer,DemuraTransOnce);
//					break;/////GM862-R66451						
//			}
//		}
//		else if(DemuraIctype==0x5D) //novateck
//		{
//			if(Demura_first_package_flag==0)
//			{
//				SSD2828_W_Reg(OLED.SigMode,channel,0xBC,180*3+1);  
//				SSD2828_W_Reg(OLED.SigMode,channel,0xBD,0x0000); 		
//				//SSD2828_W_Reg(OLED.SigMode,CS_Master,0xBE,(u16) (180*3));
//				SSD2828_W_Cmd(OLED.SigMode,channel,0xBF);
//				SSD2828_W_Data(OLED.SigMode,channel,((buffer[0] << 8) + 0x4c));  //模拟4C 
//				Demura_first_package_flag=1;
//			}
//			else
//			{
//				SSD2828_W_Reg(OLED.SigMode,channel,0xBC,180*3+1);  	
//				SSD2828_W_Cmd(OLED.SigMode,channel,0xBF);
//				SSD2828_W_Data(OLED.SigMode,channel,((buffer[0] << 8) + 0x5c)); 
//			}											
//			SPI_CS_Select(channel,0);
//			SSD2828_W_RAM_demura_buffer_part_bgr(OLED.pixel_clk,OLED.SigMode,buffer,DemuraTransOnce/3,1,0);
//			SPI_CS_Select(channel,1);										
//		}
//		else  //raydium & viewtrix
//		{			
//					SSD2828_W_Reg(OLED.SigMode,channel,0xBC,(u16) (VCP_Receive_True_num)); 
//					SSD2828_W_Reg(OLED.SigMode,channel,0xBD,(u16)((VCP_Receive_True_num)>>16)); 					
//					SSD2828_W_Reg(OLED.SigMode,channel,0xBE,(u16)(VCP_Receive_True_num/2));
//					SSD2828_W_Cmd(OLED.SigMode,channel,0xBF);
//					if(test0==0)
//					{
//							test0=1;
//							SSD2828_W_Cmd(OLED.SigMode,channel,0x2C);	
//					}
//					else
//					{
//							SSD2828_W_Cmd(OLED.SigMode,channel,0x3C);	
//					}
//					SPI_CS_Select(channel,0);
//					SSD2828_W_RAM_demura_buffer_part_rgb(OLED.pixel_clk,OLED.SigMode,buffer,DemuraTransOnce/3,1,0);
//					SPI_CS_Select(channel,1);			
//		}							
//		VCP_Receive_True_num=0;
//		
//		if(DemuraRecivedNum==USB_Rx_Demura_Total_Num)//最后一笔，状态重置
//		{
//			Demura_START_FLAG=0;  //清除Demura状态
//			
//			DemuraRecivedNum=0; //累计接收统计归零
//			USB_Rx_Demura=0;//进入Demura downloading标志
//			Demura_first_package_flag=0; //Novatek_NT37700C 4c/5c
//			//SSD2828_W_Reg(OLED.SigMode,CS_Master,0xC0,0x0001);
//		//	STM2PC_RM671xx(pdev,CDC_IN_EP,buf2PC,5+3);           //返回上位机5个byte数据
//		}
//	}
//}


void DemuraData2DDICRam(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8 buffer[],u16 LP_B7_Data,u16 HS_B7_Data)
{
			
	if(USB_Rx_Demura==0)//第一笔数据
	{	
		SSD2828_W_Reg(OLED.SigMode,channel,0xBC,(u16) (USB_Rx_Demura_Total_Num)); 
		SSD2828_W_Reg(OLED.SigMode,channel,0xBD,(u16)((USB_Rx_Demura_Total_Num)>>16)); 											
		if(OLED.H_pixel*3<=4095)//2828 command单笔上限
		{
			SSD2828_W_Reg(OLED.SigMode,channel,0xBE,(u16) (OLED.H_pixel*3)); 									
		}			
		else
		{						
			SSD2828_W_Reg(OLED.SigMode,channel,0xBE,(u16)(OLED.H_pixel));
		}					
		SSD2828_W_Cmd(OLED.SigMode,channel,0xBF);					
		{
			SSD2828_W_Cmd(OLED.SigMode,channel,0x2C);
		}
		USB_Rx_Demura=1; //第一次进入Demura数据写入状态														
	}
	//----------------------------------	
	for(i=0;i<USB_ReceivedCount;i++)
	{
		buffer[i+VCP_Receive_True_num] = USB_Rx_Buffer[i];  //
	}
	VCP_Receive_True_num+=USB_ReceivedCount;  //统计是否攒够一行
	DemuraRecivedNum+=USB_ReceivedCount;	  //统计总的接收数量	
	
	if((VCP_Receive_True_num==OLED.H_pixel*3)||(DemuraRecivedNum==USB_Rx_Demura_Total_Num)) //写入单笔数据,单次写入OLED.H_pixel*3字节
	{																					//最后一笔会多送数据，但是多送的数据不会被IC接收	

		{	//写入一行数据											
			SPI_CS_Select(channel,0);
			SSD2828_W_RAM_demura_buffer_part_rgb(OLED.pixel_clk,OLED.SigMode,buffer,OLED.H_pixel,1,0);
			SPI_CS_Select(channel,1);										
		}							
		VCP_Receive_True_num=0;
		
		if(DemuraRecivedNum==USB_Rx_Demura_Total_Num)//最后一笔，状态重置
		{
			DemuraRecivedNum=0; //清空 累计接受数据
			USB_Rx_Demura=0;	  //清空 Demura downloading状态
			Demura_START_FLAG=0;//清空 开始Demura传输的标志位
		}
	}
}
//***************************SD5202************************************************
